unit modSound;

interface

uses fmoddyn, fmodtypes;

procedure msndLoadLibrary;
function msndInit(Output, Drivers: integer): boolean;
procedure msndDeinit;
procedure msndUnloadLibrary;


var
   FMOD_LibraryLoaded: boolean;
   FMOD_Initialized  : boolean;

implementation

procedure msndUnloadLibrary;
begin
   if FMOD_LibraryLoaded then begin
      FMOD_Unload;
      FMOD_LibraryLoaded := false;
   end;
end;

procedure msndLoadLibrary;
begin
   if FMOD_LibraryLoaded then begin
      // déjà chargé
      exit;
   end;

   FMOD_LibraryLoaded := FMOD_Load('fmod.dll');
   FMOD_Initialized   := false;
end;

procedure msndDeinit;
begin
    if FMOD_Initialized then begin
       FSOUND_Close;
       FMOD_Initialized := false;
    end;
end;

function msndInit(Output, Drivers: integer): boolean;
begin
    Result := false;

    if NoT(FMOD_LibraryLoaded) then begin
       msndLoadLibrary;
       if NoT(FMOD_LibraryLoaded) then begin
          // impossible de charger FMOD
          exit;
       end;
    end;

    if FMOD_Initialized then begin
       // déjà intialisé
       exit;
    end;

   { Set the output type }
   FSOUND_SetOutput(TFSoundOutputTypes(Output));

   { Set the driver }
   FSOUND_SetDriver(Drivers);

   { Set the mixer }
   FSOUND_SetMixer(FSOUND_MIXER_QUALITY_AUTODETECT);

   { Initialise FMOD 32 software channels and no special flags }
   FMOD_Initialized := FSOUND_Init(44100, 32, 0);
   Result := FMOD_Initialized;
end;



end.
